<template>
  <view class="department-container">
    <view class="department-item" @click="toggleSubDepartments">
      <text class="department-name" @click="navigateToDepartment(department)">{{ department.departmentsName }}</text>
      <span v-if="department.subDepartments && department.subDepartments.length" :class="{'expanded': expanded}" class="toggle-icon">
        <text v-if="expanded">▲</text>
        <text v-else>▼</text>
      </span>
    </view>
    <view class="sub-departments" v-show="expanded && department.subDepartments && department.subDepartments.length">
      <department-item
        v-for="(subDepartment, subIndex) in department.subDepartments"
        :key="subIndex"
        :department="subDepartment"
        @select-sub-department="selectSubDepartment"
        class="department-subitem"
      />
    </view>
  </view>
</template>


<script>
import {findByDepartmentsId} from "@/api/doctor/doctor";


export default {
  name: 'DepartmentItem',
  props: {
    department: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      expanded: false
    };
  },
  methods: {
    navigateToDepartment(department) {
      this.$emit('select-sub-department', department.departmentsId);
    },
    toggleSubDepartments() {
      this.expanded =!this.expanded;
    },
    selectSubDepartment(subDepartment) {
      this.$emit('select-sub-department', subDepartment);
    }
  }
};
</script>


<style>
.department-container {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  padding: 20px;
}


.department-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 18px;
  color: #333;
  background: linear-gradient(135deg, #fff, #f7f7f7);
  border: 1px solid #dcdcdc;
  border-radius: 12px;
  box-shadow: 0 6px 12px rgba(0, 0, 0, 0.1);
  padding: 20px;
  margin-bottom: 20px;
  cursor: pointer;
  transition: all 0.3s ease-in-out;
}


.department-item:hover {
  box-shadow: 0 8px 16px rgba(0, 0, 0, 0.2);
  border-color: #007aff;
  background: linear-gradient(135deg, #fff, #f0f0f0);
}


.department-name {
  flex-grow: 1;
  margin-left: 20px;
}


.toggle-icon {
  margin-left: 10px;
  cursor: pointer;
  color: #007aff;
  font-size: 20px;
  transition: transform 0.3s ease-in-out;
}


.expanded {
  transform: rotate(180deg);
}


.sub-departments {
  display: none;
  flex-direction: column;
  margin-left: 40px;
  padding-top: 20px;
  border-left: 2px dashed #dcdcdc;
  padding-left: 30px;
}


.department-item.expanded.sub-departments {
  display: flex;
}


.department-subitem {
  padding: 15px 0;
  cursor: pointer;
  transition: background-color 0.3s ease-in-out;
  border-bottom: 1px solid #e0e0e0;
}


.department-subitem:last-child {
  border-bottom: none;
}


.department-subitem:hover {
  background-color: #f5f5f5;
}


